home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / stdcpp / iosystem.c < prev    next >
Text File  |  1997-03-15  |  3KB  |  139 lines

  1. /* 
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this library; see the file COPYING.  If not, write to the Free
  17. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. /*  written by Klaus Gebhardt (gebhardt@crunch.ikp.physik.th-darmstadt.de),
  26.     1996 - 1997 */
  27.  
  28. #ifdef __EMX__
  29. #include <stdlib.h>
  30. #include <process.h>
  31. #include <fcntl.h>
  32. #include <signal.h>
  33. #include <string.h>
  34. #endif
  35.  
  36. #ifdef __EMX__
  37. static char **_IO_split_args (const char *command, char *cmd, int *count)
  38. {
  39.   char *c;
  40.   char **cmd_list;
  41.  
  42.   strcpy (cmd, command);
  43.   cmd_list = _splitargs (cmd, count);
  44.  
  45.   if ((count > 0) && (cmd_list != NULL))
  46.     {
  47.       c = cmd_list[0];
  48.  
  49.       while (*c && (*c != ' ') && (*c != '\t')
  50.          && (*c != '\r') && (*c != '\n') && (*c != ''))
  51.     {
  52.       if (*c == '/')  *c = '\\';
  53.       c++;
  54.     }
  55.     }
  56.  
  57.   return cmd_list;
  58. }
  59.  
  60. static char *_IO_os2_paths (const char *command, char *cmd)
  61. {
  62.   char *c;
  63.  
  64.   strcpy (cmd, command);
  65.  
  66.   c = cmd;
  67.   while (*c && (*c != ' ') && (*c != '\t')
  68.      && (*c != '\r') && (*c != '\n') && (*c != ''))
  69.     {
  70.       if (*c == '/')  *c = '\\';
  71.       c++;
  72.     }
  73.  
  74.   return cmd;
  75. }
  76.  
  77. static char *_IO_unix_paths (const char *command, char *cmd)
  78. {
  79.   char *c;
  80.  
  81.   strcpy (cmd, command);
  82.  
  83.   c = cmd;
  84.   while (*c && (*c != ' ') && (*c != '\t')
  85.      && (*c != '\r') && (*c != '\n') && (*c != ''))
  86.     {
  87.       if (*c == '\\')  *c = '/';
  88.       c++;
  89.     }
  90.  
  91.   return cmd;
  92. }
  93.  
  94. int _IO_system(const char *command, int p_mode)
  95. {
  96.   int pid = -1;
  97.   int count;
  98.   char **cmd_list;
  99.   char cmd[512];
  100.  
  101.   cmd_list = _IO_split_args (command, cmd, &count);
  102.  
  103.   if ((p_mode == P_PM) || (p_mode == P_SESSION))
  104.     if ((count > 0) && (cmd_list != NULL))
  105.       {
  106.     pid = _spawnvp (p_mode, cmd_list[0], cmd_list);
  107.     free (cmd_list);
  108.       }
  109.  
  110.   if (pid == -1)
  111.     {
  112.       const char *sh, *base, *opt;
  113.  
  114.       if ((sh = getenv ("EMXSHELL")) == NULL)  sh = getenv ("COMSPEC");
  115.  
  116.       if (sh != NULL)
  117.     {
  118.       base = _getname (sh);
  119.  
  120.       if (stricmp(base, "cmd.exe") == 0 || stricmp(base, "4os2.exe") == 0)
  121.         {
  122.           opt = "/c";
  123.           _IO_os2_paths (command, cmd);
  124.         }
  125.       else
  126.         {
  127.           opt = "-c";
  128.           _IO_unix_paths (command, cmd);
  129.         }
  130.  
  131.       pid = _spawnlp (p_mode, sh, sh, opt, cmd, NULL);
  132.     }
  133.     }
  134.  
  135.   return (pid);
  136. }
  137. #endif
  138.  
  139.